<?php

namespace App\Http\Controllers\OperationManagement;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\OperationManagement\SanitationSite;
use App\Models\OperationManagement\SanitationCategory;
use App\Models\OperationManagement\SanitationSiteFile;
use App\Models\OperationManagement\DomesticTreatmentPlant;
use App\Models\SystemManagement\Organization;
use App\Models\SystemManagement\Employee;
use Illuminate\Database\QueryException;
use App\Http\Requests\OperationManagement\SanitationSiteRequest;
use Grimzy\LaravelMysqlSpatial\Types\Point;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

/**
 * @group 环卫部件导入->环卫作业图->生活垃圾处理厂
 *
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */
class DomesticTreatmentPlantsController extends Controller
{
    /**
     * 生活垃圾处理厂列表---分页
     *
     * @bodyParam name string 名称
     * @bodyParam status int 状态
     * @bodyParam category_id int  required 站点类型id (生活垃圾处理厂类型=1)
     * @bodyParam location int 状态
     *
     * @queryParam name string 站点名称
     * @queryParam number string 编号
     * @queryParam category_id int 站点类型 外键 站点类型表
     * @queryParam category->name string 站点类型
     * @queryParam address string 地址
     * @queryParam location string 经纬度-点 115.24556,25.14551
     * @queryParam handle int 日处理量
     * @queryParam property_company string 产权单位
     * @queryParam operate_company string 运营单位
     * @queryParam employees->name string 负责人 外键 员工表
     * @queryParam phone string 电话
     * @queryParam status int 厂房状态
     * @queryParam rfid int RFID绑定
     * @queryParam domestic_treatment_plant->imei string 设备唯一imei
     * @queryParam domestic_treatment_plant->name string 设备名称
     * @queryParam domestic_treatment_plant->address string 地址
     * @queryParam domestic_treatment_plant->location string 经纬度-点 115.24556,25.14551
     * @queryParam domestic_treatment_plant->temperature string 炉温
     * @queryParam domestic_treatment_plant->incoming string 来料
     * @queryParam domestic_treatment_plant->gas string 尾气
     * @queryParam domestic_treatment_plant->smell string 气味
     * @queryParam domestic_treatment_plant->slag string 炉渣
     * @queryParam domestic_treatment_plant->water string 用水
     * @queryParam domestic_treatment_plant->electric string 用电
     * @queryParam files->file->local_url string 附件url地址
     */
    public function index(Request $request)
    {
        $sanitationSite = new SanitationSite;
        $data = $sanitationSite->getOtherList($request);
        return response()->json(encodeMsg($data,200));
    }

    /**
     * 新增生活垃圾处理厂硬件数据
     *
     *
     * @bodyParam imei string 设备唯一imei
     * @bodyParam name string 设备名称
     * @bodyParam address string 地址
     * @bodyParam location string 经纬度-点 115.24556,25.14551
     * @bodyParam temperature string 炉温
     * @bodyParam incoming string 来料
     * @bodyParam gas string 尾气
     * @bodyParam smell string 气味
     * @bodyParam slag string 炉渣
     * @bodyParam water string 用水
     * @bodyParam electric string 用电
     */
    public function store(Request $request)
    {
        try {
            $data = $request->all();
            $location = $request->input('location','');
            if(!empty($location)){
                $data['location'] = Point::fromString($location);
            }
            $domesticTreatmentPlant = DomesticTreatmentPlant::create($data);
            return response()->json(encodeMsg($domesticTreatmentPlant,201));
        } catch (QueryException $e) {
            // dd($e);
            return response()->json(encodeMsg(['message' => '添加失败!'],400));
        }
    }

    /**
     * 查看生活垃圾处理厂
     *
     * @bodyParam id int required 站点ID
     * @bodyParam category_id int  required 站点类型id (生活垃圾处理厂类型=1)
     *
     * @queryParam name string 站点名称
     * @queryParam number string 编号
     * @queryParam category_id int 站点类型 外键 站点类型表
     * @queryParam category->name string 站点类型
     * @queryParam address string 地址
     * @queryParam location string 经纬度-点 115.24556,25.14551
     * @queryParam handle int 日处理量
     * @queryParam property_company string 产权单位
     * @queryParam operate_company string 运营单位
     * @queryParam employees->name string 负责人 外键 员工表
     * @queryParam phone string 电话
     * @queryParam status int 厂房状态
     * @queryParam rfid int RFID绑定
     * @queryParam domestic_treatment_plant->imei string 设备唯一imei
     * @queryParam domestic_treatment_plant->name string 设备名称
     * @queryParam domestic_treatment_plant->address string 地址
     * @queryParam domestic_treatment_plant->location string 经纬度
     * @queryParam domestic_treatment_plant->temperature string 炉温
     * @queryParam domestic_treatment_plant->incoming string 来料
     * @queryParam domestic_treatment_plant->gas string 尾气
     * @queryParam domestic_treatment_plant->smell string 气味
     * @queryParam domestic_treatment_plant->slag string 炉渣
     * @queryParam domestic_treatment_plant->water string 用水
     * @queryParam domestic_treatment_plant->electric string 用电
     * @queryParam files->file->local_url string 附件url地址
     */
    public function show(Request $request,$id)
    {
        $category_id = $request->input('category_id');
        $data = SanitationSite::with(['category','employees','domestic_treatment_plant','files'])->where('category_id',$category_id)->find($id);
        if($data == []) {
            return response()->json(encodeMsg(["message" => "没有该生活垃圾处理厂信息!"], 404));
        } else {
            return response()->json(encodeMsg($data,200));
        }
    }

    /**
     * 修改生活垃圾处理厂硬件数据
     *
     * @bodyParam id int required 站点ID
     *
     * @queryParam imei string 设备唯一imei
     * @queryParam name string 设备名称
     * @queryParam address string 地址
     * @queryParam location string 经纬度-点 115.24556,25.14551
     * @queryParam temperature string 炉温
     * @queryParam incoming string 来料
     * @queryParam gas string 尾气
     * @queryParam smell string 气味
     * @queryParam slag string 炉渣
     * @queryParam water string 用水
     * @queryParam electric string 用电
     */
    public function update(Request $request, $id)
    {
        $domesticTreatmentPlant = DomesticTreatmentPlant::find($id);
        if($domesticTreatmentPlant == []) {
            return response()->json(encodeMsg(["message" => "没有该生活垃圾处理厂硬件信息!"], 404));
        }
        try {
            $data = $request->all();
            $location = $request->input('location','');
            if(!empty($location)){
                $data['location'] = Point::fromString($location);
            }
            $domesticTreatmentPlant->update($data);
            return response()->json(encodeMsg($domesticTreatmentPlant,200));
        } catch (QueryException $e) {
            return response()->json(encodeMsg(['message' => '修改失败!'],400));
        }
    }

    /**
     * 删除生活垃圾处理厂硬件数据
     *
     *
     * @bodyParam id int required  站点ID
     * @response 204 {
     *   "message": "数据删除成功"
     * }
     *
     *
     * @response 404 {
     *  "message": "没有该数据信息"
     * }
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $domesticTreatmentPlant = DomesticTreatmentPlant::find($id);
        if($domesticTreatmentPlant == []) {
            return response()->json(encodeMsg(["message" => "没有该数据信息!"], 404));
        }
        if($domesticTreatmentPlant->delete()) {
            return response()->json(encodeMsg(["message" => "数据删除成功!"], 204));
        } else {
            return response()->json(encodeMsg(["message" => "数据删除失败"], 400));
        }
    }

    /**
     * 批量删除生活垃圾处理厂硬件数据
     *
     * @bodyParam ids int required id集合字符串[格式：ids=1,2,3]
     * @response 204 {
     *   "message": "生活垃圾处理厂硬件数据删除成功"
     * }
     *
     *
     */
    public function delete($ids)
    {
        $listId = explode(',', $ids);
        DomesticTreatmentPlant::whereIn('id',$listId)->delete();
        return response()->json(encodeMsg(["message" => "删除成功!"], 204));
    }
}
